package com.github.baichuan.application.scaffold.controller;

import com.github.baichuan.application.scaffold.domain.app.AppVersion;
import com.github.baichuan.application.scaffold.domain.app.AppVersionService;
import com.github.baichuan.application.scaffold.domain.app.UpgradeMode;
import com.github.baichuan.application.scaffold.dto.app.*;
import com.github.baichuan.application.scaffold.jpa.app.AppVersionSpecification;
import com.github.baichuan.mapping.EnumDescriptionMapper;
import com.github.baichuan.web.dto.Pagination;
import com.github.baichuan.web.dto.PaginationCriteria;
import com.github.baichuan.web.dto.Result;
import com.github.baichuan.web.dto.SelectOption;
import com.github.baichuan.web.servlet.Restful;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Optional;

@Tag(name = "APP程序版本")
@RestController
@RequestMapping("/app/version")
public class AppVersionController {
    @Autowired
    private AppVersionService versionService;

    @Operation(summary = "版本更新检测")
    @PostMapping("/upgrade")
    public ResponseEntity<Optional<UpgradeVersion>> upgrade(@RequestHeader("x-application-code") @Parameter(hidden = true) String application,
                                                            @RequestBody @Validated CurrentVersion version){
        Optional<UpgradeVersion> upgradeVersion = versionService.upgrade(application, version);
        return ResponseEntity.ok(upgradeVersion);
    }

    @Operation(summary = "查询")
    @PostMapping("/query")
    public ResponseEntity<Pagination<AppVersionTableView>> query(@RequestBody PaginationCriteria<AppVersionQueryCriteria> paginationCriteria){
        AppVersionSpecification specification = paginationCriteria.getCriteria().specification();
        Page<AppVersion> page = versionService.query(specification, paginationCriteria.pageable(AppVersionTableView.class));
        AppVersionTableView.Mapper mapper = new AppVersionTableView.Mapper();
        Pagination<AppVersionTableView> pagination = Pagination.of(page).map(x -> mapper.map(x));
        return ResponseEntity.ok(pagination);
    }

    @Operation(summary = "发布版本")
    @PostMapping("/{appId}/release")
    public ResponseEntity<Result> add(@PathVariable("appId") String appId, @RequestBody @Validated AppVersionForm form) {
        Result result = versionService.add(appId, form);
        return Restful.created(result);
    }

    @Operation(summary = "删除版本")
    @DeleteMapping("/{id}")
    public ResponseEntity<Result> remove(@PathVariable("id") int id){
        Result result = versionService.remove(id);
        return Restful.deleted(result);
    }

    @GetMapping("/upgrade-mode/options")
    @Operation(summary = "升级方式")
    public ResponseEntity<List<SelectOption<String>>> upgradeMode() throws IllegalAccessException {
        List<SelectOption<String>> options = EnumDescriptionMapper.options(UpgradeMode.class);
        return ResponseEntity.ok(options);
    }
}
